package com.yisu.expressway.utils;


import android.content.Context;
import android.widget.Toast;

import com.yisu.expressway.application.ExApplication;

/**
 * Utility functions for the application.
 */
public class ToastUtil {
    private static Toast toast = null;

    /**
     * Displays the short toast message.
     *
     * @param context       The app context.
     * @param toastMsgShort The short toast message to be displayed.
     */
    public static void displayShortToastMsg(Context context, String toastMsgShort) {
        try {
            if (toast == null) {
                toast = Toast.makeText(context.getApplicationContext(), toastMsgShort, Toast.LENGTH_SHORT);
            } else {
                try {
                    toast.setText(toastMsgShort);
                } catch (Exception e) {
                    Toast.makeText(context.getApplicationContext(), toastMsgShort, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            toast.show();
        } catch (Exception ex) {
        }
    }

    public static void displayShortToastMsg(String toastMsgShort){
        try {
            if (toast == null) {
                toast = Toast.makeText(ExApplication.getInstance().getApplicationContext(), toastMsgShort, Toast.LENGTH_SHORT);
            } else {
                try {
                    toast.setText(toastMsgShort);
                } catch (Exception e) {
                    Toast.makeText(ExApplication.getInstance().getApplicationContext(), toastMsgShort, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            toast.show();
        } catch (Exception ex) {
        }
    }

    public static void displayShortToastMsg(Context context, int stringId) {
        try {
            if (toast == null) {
                toast = Toast.makeText(context.getApplicationContext(), context.getResources().getString(stringId), Toast.LENGTH_SHORT);
            } else {
                try {
                    toast.setText(context.getResources().getString(stringId));
                } catch (Exception e) {
                    Toast.makeText(context.getApplicationContext(), context.getResources().getString(stringId), Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            toast.show();
        } catch (Exception ex) {
        }
    }

    public static void displayShortToastMsg(int stringId) {
        try {
            if (toast == null) {
                toast = Toast.makeText(ExApplication.getInstance().getApplicationContext(), ExApplication.getInstance().getResources().getString(stringId), Toast.LENGTH_SHORT);
            } else {
                try {
                    toast.setText(ExApplication.getInstance().getResources().getString(stringId));
                } catch (Exception e) {
                    Toast.makeText(ExApplication.getInstance().getApplicationContext(), ExApplication.getInstance().getResources().getString(stringId), Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            toast.show();
        } catch (Exception ex) {
        }
    }

    public static void showMessage(final Context act, final String msg) {
        showMessage(act, msg, Toast.LENGTH_SHORT);
    }

    public static void showLongMessage(final Context act, final String msg) {
        showMessage(act, msg, Toast.LENGTH_LONG);
    }

    public static void showMessage(final Context act, final String msg,
                                   final int len) {
        try {
            if (toast == null) {
                toast = Toast.makeText(act.getApplicationContext(), msg, len);
            } else {
                try {
                    toast.setText(msg);
                } catch (Exception e) {
                    Toast.makeText(act.getApplicationContext(), msg, len).show();
                    return;
                }

            }
            toast.show();
        } catch (Exception ex) {
        }
    }

    /**
     * Displays the long toast message.
     *
     * @param context      The app context.
     * @param toastMsgLong The long toast message to be displayed.
     */
    public static void displayLongToastMsg(Context context, String toastMsgLong) {
        try {
            if (toast == null) {
                toast = Toast.makeText(context.getApplicationContext(), toastMsgLong, Toast.LENGTH_LONG);
            } else {
                try {
                    toast.setText(toastMsgLong);
                } catch (Exception e) {
                    Toast.makeText(context.getApplicationContext(), toastMsgLong, Toast.LENGTH_LONG).show();
                    return;
                }
            }
            toast.show();
        } catch (Exception ex) {
        }
    }

    public static void displayLongToastMsg(Context context, int stringId) {
        try {
            if (toast == null) {
                toast = Toast.makeText(context.getApplicationContext(), context.getResources().getString(stringId), Toast.LENGTH_LONG);
            } else {
                try {
                    toast.setText(context.getResources().getString(stringId));
                } catch (Exception e) {
                    Toast.makeText(context.getApplicationContext(), context.getResources().getString(stringId), Toast.LENGTH_LONG).show();
                    return;
                }
            }
            toast.show();
        } catch (Exception ex) {
        }
    }
}
